//GameStateBattle Class
#include "GameStateBattle.h"
#include "GameStateManager.h"
#include "GameStateMap.h"


//GameStateBattle Instance
GameStateBattle GameStateBattle::Instance;

//default constructor
GameStateBattle::GameStateBattle() {
    Surf_Logo = NULL;
}

//OnKeyDown
void GameStateBattle::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    //bool swapState = false;

    switch(sym) {

        case SDLK_SPACE: {
            //swapState = true;
            break;
        }

        default: {
        }
    }
}

//OnKeyUp
void GameStateBattle::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
    switch(sym) {
        case SDLK_SPACE: {
            if(playerWon)
                GameStateManager::SetActiveGameState(GAMESTATE_MAP);
            break;
        }
        case SDLK_q: {
            GameStateManager::SetActiveGameState(0);
            break;
        }

        default: {
        }
    }
}

//OnActivate-
void GameStateBattle::OnActivate() {
    // Load Simple Logo
    Surf_Logo = Surface::OnLoad("battle.png");
    Dragon = Surface::OnLoad("dragon.png");
    Hero = Surface::OnLoad("sprites.png");
    //Load font
    Surf_Font = tx.OnLoad("VeraBd.ttf", 16);
    //Set font color
    fColor.r = 255;
    fColor.g = 0;
    fColor.b = 0;
    bColor.r = bColor.g = bColor.b = 75;
    Player1.SetCurrHealth(50);
    Enemy1.SetCurrHealth(50);
    playerWon = false;
    isPlayerTurn = true;
    isInitialized = false;
    pHP.h = 50;
    pHP.w = 200;
    pHP.x = 15;
    pHP.y = 420;
    eHP.h = 50;
    eHP.w = 200;
    eHP.x = 415;
    eHP.y = 420;
    hitbox.h = 30;
    hitbox.w = 300;
    hitbox.x = 10;
    hitbox.y = 10;
}

//OnDeactivate
void GameStateBattle::OnDeactivate() {
    if(Surf_Logo) {
        SDL_FreeSurface(Surf_Logo);
    }
    Surf_Logo = NULL;
    //this creates a black box to cover old graphics (due to bug?)
    SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);
    //Close font
    TTF_CloseFont(Surf_Font);
}

//OnLoop
void GameStateBattle::OnLoop() {
        srand(time(NULL));
        random = rand()%3;
        if(!isInitialized)
        {
                out << Enemy1.GetCurrHealth();
                e.clear();
                out >>e;
                out << Player1.GetCurrHealth();
                s.clear();
                out >>s;
        }
        if(!playerWon){
            out.clear();
            if(!isPlayerTurn){

                int PHealth = Player1.GetCurrHealth() - Enemy1.GetBaseDamage() + random;
                Player1.SetCurrHealth(PHealth);
                out << Player1.GetCurrHealth();
                e.clear();
                out >> e;
                out.clear();
                out << Enemy1.GetBaseDamage() + random;
                d.clear();
                out >> d;
                isPlayerTurn = true;
            }
            else{

                int EHealth = Enemy1.GetCurrHealth()-Player1.GetBaseDamage() - random;
                Enemy1.SetCurrHealth(EHealth);
                out << Enemy1.GetCurrHealth() - random;
                s.clear();
                out >> s;
                out.clear();
                out << Player1.GetBaseDamage() - random;
                d.clear();
                out >> d;
                isPlayerTurn = false;
            }
        }

        if(Enemy1.GetCurrHealth() < 1){
            playerWon = true;

        }

        SDL_Delay(800);

}

//OnRender
void GameStateBattle::OnRender(SDL_Surface* Surf_Display) {
    if(!isInitialized)
    {
        tx.OnRender(Surf_Display, Surf_Font, 50, 400, "Player HP");
        tx.OnRender(Surf_Display, Surf_Font, 500, 400, "Enemy HP");
        tx.OnRender(Surf_Display, Surf_Font, 500, 425, (char*)e.c_str(), fColor);
        tx.OnRender(Surf_Display, Surf_Font, 50, 425, (char*)s.c_str(), fColor);
        if(Surf_Logo) {
            Surface::OnDraw(Surf_Display, Surf_Logo, 0, 0);
        }
        if(Dragon) {
            Surface::OnDraw(Surf_Display, Dragon, 420, 255);
        }
        if(Hero) {
            Surface::OnDraw(Surf_Display, Hero, 150, 305, 65, 65, 32, 32);
        }
        isInitialized=true;
    }


    if(isPlayerTurn){
        SDL_FillRect(Surf_Display, &pHP, SDL_MapRGB(Surf_Display->format, 0, 0, 0));
        SDL_FillRect(Surf_Display, &hitbox, SDL_MapRGB(Surf_Display->format, 0, 0, 0));
        tx.OnRender(Surf_Display, Surf_Font, 50, 425, (char*)e.c_str(), fColor);
        tx.OnRender(Surf_Display, Surf_Font, 10, 10, "Player hit enemy for ");
        tx.OnRender(Surf_Display, Surf_Font, 205, 10, (char*)d.c_str(), fColor);
        tx.OnRender(Surf_Display, Surf_Font, 225, 10, "damage");
    }
    else{
        SDL_FillRect(Surf_Display, &eHP, SDL_MapRGB(Surf_Display->format, 0, 0, 0));
        SDL_FillRect(Surf_Display, &hitbox, SDL_MapRGB(Surf_Display->format, 0, 0, 0));
        tx.OnRender(Surf_Display, Surf_Font, 500, 425, (char*)s.c_str(), fColor);
        tx.OnRender(Surf_Display, Surf_Font, 10, 10, "Enemy hit player for ");
        tx.OnRender(Surf_Display, Surf_Font, 205, 10, (char*)d.c_str(), fColor);
        tx.OnRender(Surf_Display, Surf_Font, 225, 10, "damage");
    }

    if(playerWon){
        tx.OnRender(Surf_Display, Surf_Font, 200, 50, "You Win!");
        tx.OnRender(Surf_Display, Surf_Font, 200, 75, "Press Space to Continue");
        SDL_FillRect(Surf_Display, &eHP, SDL_MapRGB(Surf_Display->format, 0, 0, 0));
        tx.OnRender(Surf_Display, Surf_Font, 500, 425, "0", fColor);
    }
}

//GetInstance
GameStateBattle* GameStateBattle::GetInstance() {
    return &Instance;
}


